﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dot5Demo
{
    public class BaseCtr : Control, IRender
    {
        public BaseCtr()
        {
            Renders = new List<IRender>();
        }

        public IList<IRender> Renders { get; set; }
        public Color BackCOlor { get; set; }
        public Color ForeCOlor { get; set; }

        public void AddRenderer(IRender renderer)
        {
            if (Exist(renderer))
                return;
            Renders.Add(renderer);
        }

        public void ClearRenderer()
        {
            if (Renders == null || Renders.Count == 0)
                return;
            Renders.Clear();
        }

        public void RemoveRenderer(IRender renderer)
        {
            if (!Exist(renderer))
                return;
            Renders.Remove(renderer);
        }

        public bool Exist(IRender renderer)
        {
            if (renderer == null)
                return false;
            if (Renders == null || Renders.Count == 0)
                return false;
            return Renders.Contains(renderer);
        }

        public void DoRender(Graphics g, Rectangle rect)
        {
            g.DrawRectangle(Pens.Green, new Rectangle(2, 2, Width - 4, Height - 4));
        }

        public void DoRender(Graphics g, RectangleF rect)
        {
            g.DrawRectangle(Pens.Red, new Rectangle(2, 2, Width - 4, Height - 4));
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.DoRender(e.Graphics, this.Bounds);
            if (Renders == null || Renders.Count == 0)
                return;
            for (int i = 0; i < Renders.Count; i++)
            {
                if (Renders[i] == null)
                    continue;
                Renders[i].DoRender(e.Graphics, new Rectangle(0, 0, Width, Height));
            }
        }
    }
}
